<?php
global $dbo;
$dbo = new dbhelper;
class dbhelper
{
	var $pconnect = false;  
	var $nconnect = true;  
	var $host;              
	var $user;             
	var $pwd;               
	var $dbname;            
	var $dbo;             
	var $pre;				
	var $result;           
	
	function __construct()
	{
		$this->host = $GLOBALS['database']['dbserver'];    
		$this->user = $GLOBALS['database']['dbuser'];	      
		$this->pwd = $GLOBALS['database']['dbpass'];     
		$this->dbname = $GLOBALS['database']['dbname'];
		$this->pre = $GLOBALS['database']['dbpre'];            
		if($this->nconnect)           
		{
			$this->dbo = mysqli_connect($this->host,$this->user,$this->pwd,$this->dbname);  
			mysqli_query($this->dbo,'set names utf8');
		}
	}
	function execute($sql)
	{
		$sql = str_replace('@db__',$this->pre,$sql); 		
		$this->result = mysqli_query($this->dbo,$sql); 		
	}
	function select($sql)
	{		
		$data = array();
		$this->execute($sql);
		if($this->result)
		{
			while($row = mysqli_fetch_array($this->result))
			{
				$data[] = $row; 
			}
		}
		return $data;
	}
	function single($sql)
	{
		$data = array();
		$this->execute($sql);
		$data = mysqli_fetch_assoc($this->result);
		return $data;
	}
	function clear($tab,$id = 0)
	{
		$int = 0;
		$sql = 'delete from `@db__' . $tab .'` where uid=' . $id;
		$this->execute($sql);
		$int = mysqli_affected_rows($this->dbo);
			
	}
	function purge($tab,$id = 0,$type = 1)
	{
		$int = 0;
		$sql = '';
		if($type == 1)
		{
			$sql = 'delete from `@db__' . $tab .'` where id=' . $id;
			$this->execute($sql);
			$int = mysqli_affected_rows($this->dbo);
		}
		else if($type == 2)
		{
			$sql = 'delete from `@db__' . $tab .'` where id in (' . $id . ')';
			$this->execute($sql);
			$int = mysqli_affected_rows($this->dbo);
		}
		else if($type == 3)
		{
			$arr = explode(',',$id);
			foreach($arr as $v)
			{
				$sql = 'delete from `@db__' . $tab .'` where id=' . $v;
				$this->execute($sql);
				$int += mysqli_affected_rows($this->dbo);
			}
		}
		else
		{
			$sql = 'delete from `@db__' . $tab .'`';
			$this->execute($sql);
			$int = mysqli_affected_rows($this->dbo);
		}
		return $int;
	}
	function update($tab,$arr,$id)
	{
		$int = 0;
		$key = '';
		$sql = '';
		foreach($arr as $k => $v)
		{
			$key .= empty($key)?"`".$k."`='".$v."'":",`".$k."`='".$v."'";	
		}
		$sql = "update `@db__".$tab."` set ".$key." where id=".$id;
		$this->execute($sql);
		$int = mysqli_affected_rows($this->dbo);
		return $int;
	}
	function modify($tab,$arr,$id)
	{
		$int = 0;
		$key = '';
		$sql = '';
		foreach($arr as $k => $v)
		{
			$key .= empty($key)?"`".$k."`='".$v."'":",`".$k."`='".$v."'";	
		}
		$sql = "update `@db__".$tab."` set ".$key." where pid='".$id."'";

		$this->execute($sql);
		$int = mysqli_affected_rows($this->dbo);
		return $int;
	}
	function insert($tab,$arr)
	{
		$int = 0;
		$key = '';
		$value = '';
		$sql = '';
		foreach($arr as $k => $v)
		{
			$key .= empty($key)?"`" . $k ."`":",`" . $k . "`";
			$value .= empty($value)?"'".$v."'":",'" . $v . "'";
		}
		$sql = "insert into `@db__" .$tab."` (" . $key .") values (" .$value.")";
		//echo $sql;
		$this->execute($sql);
		$int = mysqli_affected_rows($this->dbo);
		return $int;
	}
	function lastid()
	{
		$sql = 'Select LAST_INSERT_ID() as lid';
		$res = $this->execute($sql);
		$row = mysqli_fetch_array($res);
		return $row['lid'];
	}
}
?>